package com.wise.service.cms.content.pagedecorate;

import com.wise.entities.content.Decorate;
import com.wise.entities.content.DecorateType;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.session.GetAcceptLanguage;
import com.wise.summer.v1.cms.content.pagedecorate.AddPageDecorate;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;

import static com.wise.model.content.ContentErrMsgs.Err_000500000027;


@Service
@SummerService
public class AddPageDecorateService implements SummerServiceBean<AddPageDecorate> {

    @Override
    public void sum(AddPageDecorate model) {
        String lang = GetAcceptLanguage.s(true);
        String userId = CurrentUserId.s(true);
        // 别名转换
        model.setAlias(PageDecorateTools.toNullAlias(model.getAlias()));
        // 别名不许重复
        if (PageDecorateTools.checkAlias(model.getAlias())) {
            throw new LogicException(this.getClass(), Err_000500000027);
        }

        Timestamp now = DateTimeUtil.timestampNow();

        Decorate decorate = new Decorate();
        decorate.setAlias(model.getAlias());
        decorate.setLang(lang);
        decorate.setType(DecorateType.PC.value());
        decorate.setModule(model.getModule());
        decorate.setTitle(model.getTitle());
        decorate.setImg(model.getImg());

        decorate.setCreatedBy(userId);
        decorate.setCreationDate(now);
        decorate.setLastUpdateBy(userId);
        decorate.setLastUpdateDate(now);

        decorate.setSeoTitle(model.getSeoTitle());
        decorate.setSeoDescribe(model.getSeoDescribe());

        decorate.setPageTitle(model.getPageTitle());

        if (model.getState() == 0) {
            //保存草稿
            decorate.setContent(model.getContent());
        } else if (model.getState() == 1) {
            decorate.setContent(model.getContent());
            decorate.setPublicContent(model.getContent());
        }

        DaoSave.s(decorate);
        model.setSummerResult(decorate);
    }

}
